﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Negocio;
namespace Persistencia
{

    class BrokerClinicalHistory : Broker
    {
        public BrokerClinicalHistory()
        {
            this.conn = ConnectionDB.getInstance();
        }

        public override void add(Object o)
        {
            ClinicalHistory clinical = (ClinicalHistory)o;
            this.conn.connect();
            ObservableCollection<Disease> diseasesList = clinical.Diseases;
            ObservableCollection<Injury> injuriesList = clinical.Injuries;
            IEnumerator<Disease> diseaseEnum = diseasesList.GetEnumerator();
            IEnumerator<Injury> injuryEnum = injuriesList.GetEnumerator();
            string sql = "insert into ClinicalHistory (oid,OidPlayer) values('" + clinical.OID + "','" + clinical.Player.OID + "')";
            this.conn.execute(sql);
            string sqlUpdateOID = "update OID set Oid=" + clinical.OID;
            this.conn.execute(sqlUpdateOID);
            while(diseaseEnum.MoveNext())
            {
                diseaseEnum.Current.added();
                string sqlSetClinicalOid = "update disease set OidClinicalHistory = " + clinical.OID;
                this.conn.execute(sqlSetClinicalOid);
            }
            while(injuryEnum.MoveNext())
            {
                injuryEnum.Current.added();
                string sqlSetClinicalOid = "update injury set OidClinicalHistory = " + clinical.OID;
                this.conn.execute(sqlSetClinicalOid);
            }
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            ClinicalHistory clinical = (ClinicalHistory)o;
            this.conn.connect();
            ObservableCollection<Disease> diseasesList = clinical.Diseases;
            ObservableCollection<Injury> injuriesList = clinical.Injuries;
            IEnumerator<Disease> diseasesEnum = diseasesList.GetEnumerator();
            IEnumerator<Injury> injuriesEnum = injuriesList.GetEnumerator();
            string sqlDeleteDiseases = "delete * from disease where oidClinicalHistory=" + clinical.OID;
            this.conn.execute(sqlDeleteDiseases);
            string sqlDeleteInjuries = "delete * from injury where oidClinicalHistory=" + clinical.OID;
            this.conn.execute(sqlDeleteInjuries);
            while (diseasesEnum.MoveNext())
            {
                diseasesEnum.Current.added();
                string sqlSetClinicalOid = "update disease set OidClinicalHistory = " + clinical.OID;
                this.conn.execute(sqlSetClinicalOid);
            }
            while (injuriesEnum.MoveNext())
            {
                injuriesEnum.Current.added();
                string sqlSetClinicalOid = "update injury set OidClinicalHistory = " + clinical.OID;
                this.conn.execute(sqlSetClinicalOid);
            }            
            this.conn.disconnect();            
        }

        public override void delete(Object o)
        {
            ClinicalHistory clinical = (ClinicalHistory)o;
            this.conn.connect();
            ObservableCollection<Disease> diseasesList = clinical.Diseases;
            IEnumerator<Disease> diseasesEnum = diseasesList.GetEnumerator();
            while (diseasesEnum.MoveNext())
            {
                diseasesEnum.Current.removed();
            }
            ObservableCollection<Injury> injuriesList = clinical.Injuries;
            IEnumerator<Injury> injuriesEnum = injuriesList.GetEnumerator();
            while (injuriesEnum.MoveNext())
            {
                injuriesEnum.Current.removed();
            }
            string sqlDeleteDiseases = "delete * from Disease where oidClinicalHistory = " + clinical.OID;
            this.conn.execute(sqlDeleteDiseases);
            
            this.conn.disconnect();
        }


        public override void bringData(Object o)
        {
                
            ClinicalHistory clinical = (ClinicalHistory)o;
            string sql = "Select * from ClinicalHistory where oid =" + clinical.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                clinical.Player.OID = int.Parse(this.conn.getDataString("OidPlayer").ToString());                  
            }
            string sqlDiseases = "Select * from disease where oidClinicalHistory =" + clinical.OID;
            this.conn.bringList(sqlDiseases);
            while (this.conn.nextRecord())
            {
                Disease disease = new Disease();
                disease.SeverityPoints = int.Parse(this.conn.getDataString("severityPoints").ToString());
                disease.Comment = this.conn.getDataString("comment").ToString().ToString();
                disease.Description = this.conn.getDataString("description").ToString();
                disease.OfBirth = (Boolean)this.conn.getDataString("ofBirth");
                disease.Name = this.conn.getDataString("name").ToString();
                disease.Date = (DateTime)this.conn.getDataString("Date");
                disease.AffectedSystem = this.conn.getDataString("AffectedSystem").ToString();
                disease.Status = this.conn.getDataString("Status").ToString();
                disease.OID = int.Parse(this.conn.getDataString("oid").ToString());
                clinical.addDisease(disease);               
            }
            string sqlInjuries = "Select * from injuries where oidClinicalHistory =" + clinical.OID;
            this.conn.bringList(sqlInjuries);
            while (this.conn.nextRecord())
            {
                Injury InjuryAux = (Injury)o;
                InjuryAux.Status = this.conn.getDataString("status").ToString();
                InjuryAux.Date = (DateTime)this.conn.getDataString("Date_");
                InjuryAux.Type = this.conn.getDataString("type").ToString();
                InjuryAux.Severity = this.conn.getDataString("severity").ToString();
                InjuryAux.Description = this.conn.getDataString("description").ToString();
                InjuryAux.AffectedArea = this.conn.getDataString("affectedArea").ToString();
                InjuryAux.ActivityWhenInjured = this.conn.getDataString("activityWhenInjured").ToString();
                InjuryAux.Name = this.conn.getDataString("Name").ToString();
                InjuryAux.Comments = this.conn.getDataString("Comments").ToString();
                InjuryAux.Cause = this.conn.getDataString("cause").ToString();
                InjuryAux.TreatmentType = this.conn.getDataString("treatmentType").ToString();
                InjuryAux.OID = int.Parse(this.conn.getDataString("oid").ToString());
                clinical.addInjury(InjuryAux);
            }
            this.conn.disconnect();           
        }

        public override ObservableCollection<Object> bringList(Object o)
        {            
            ObservableCollection<Object> list = new ObservableCollection<Object>();
            string sql = "Select * from ClinicalHistory";
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                ClinicalHistory clinical = new ClinicalHistory();
                clinical.Player.OID = int.Parse(this.conn.getDataString("OidPlayer").ToString());
                clinical.OID = int.Parse(this.conn.getDataString("Oid").ToString());
                list.Add(clinical);
            }
            IEnumerator<Object> p = list.GetEnumerator();
            while (p.MoveNext())
            {
                string sqlDiseases = "Select * from disease where OidClinicalHistory = " + ((ClinicalHistory)p.Current).OID;
                this.conn.bringList(sqlDiseases);
                while (this.conn.nextRecord())
                {
                    Disease disease = new Disease();
                    disease.SeverityPoints = int.Parse(this.conn.getDataString("severityPoints").ToString());
                    disease.Comment = this.conn.getDataString("comment").ToString().ToString();
                    disease.Description = this.conn.getDataString("description").ToString();
                    disease.OfBirth = Boolean.Parse(this.conn.getDataString("ofBirth").ToString());
                    disease.Name = this.conn.getDataString("name").ToString();
                    disease.Date = DateTime.Parse(this.conn.getDataString("date_").ToString());
                    disease.AffectedSystem = this.conn.getDataString("AffectedSystem").ToString();
                    disease.Status = this.conn.getDataString("Status").ToString();
                    disease.OID = int.Parse(this.conn.getDataString("oid").ToString());
                    ((ClinicalHistory)p.Current).addDisease(disease);
                }
                string sqlInjury = "Select * from injury where OidClinicalHistory = " + ((ClinicalHistory)p.Current).OID;
                this.conn.bringList(sqlInjury);
                while (this.conn.nextRecord())
                {
                    Injury InjuryAux = new Injury();
                    InjuryAux.Name = this.conn.getDataString("name").ToString();
                    InjuryAux.Status = this.conn.getDataString("status").ToString();
                    InjuryAux.Date = DateTime.Parse(this.conn.getDataString("date_").ToString());
                    InjuryAux.Type = this.conn.getDataString("type").ToString();
                    InjuryAux.Severity = this.conn.getDataString("severity").ToString();
                    InjuryAux.Description = this.conn.getDataString("description").ToString();
                    InjuryAux.AffectedArea = this.conn.getDataString("affectedArea").ToString();
                    InjuryAux.ActivityWhenInjured = this.conn.getDataString("activityWhenInjured").ToString();
                    InjuryAux.Cause = this.conn.getDataString("cause").ToString();
                    InjuryAux.Comments = this.conn.getDataString("Comments").ToString();
                    InjuryAux.TreatmentType = this.conn.getDataString("treatmentType").ToString();
                    InjuryAux.OID = int.Parse(this.conn.getDataString("oid").ToString());
                    ((ClinicalHistory)p.Current).addInjury(InjuryAux);                    
                }
            }

            this.conn.disconnect();
            return list;            
        }
            

    }
             
}
